package com.example.beat_x3.Adapter;

import android.annotation.SuppressLint;
import android.net.Uri;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.beat_x3.Client.GetResult;
import com.example.beat_x3.Client.LoadImages;
import com.example.beat_x3.R;

import java.util.ArrayList;

import Entity.Equipment;
import Entity.Inf;
//TODO "需改写"
public class MyInstrumentAdapter extends RecyclerView.Adapter<MyInstrumentAdapter.Holder> {
    public ArrayList<Equipment> equipments;
    public int deletePosition, changePosition, old_size;

    @NonNull
    @Override
    public MyInstrumentAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyInstrumentAdapter.Holder(LayoutInflater.from(Inf.context).inflate(R.layout.item_myinstrument, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyInstrumentAdapter.Holder holder, int position) {
        holder.bind(position);
    }

    @Override
    public int getItemCount() {
        if (equipments == null) return 0;
        return equipments.size();
    }

    public void appendNew() {
        if (getItemCount() > old_size) {
            notifyItemRangeChanged(old_size, getItemCount() - old_size);
            old_size = getItemCount();
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    public void refresh() {
        notifyDataSetChanged();
        old_size=getItemCount();
    }

    public void change() {
        notifyItemChanged(changePosition);
    }

    public void delete() {
        notifyItemRemoved(deletePosition);
        notifyItemRangeChanged(deletePosition, getItemCount() - deletePosition);
        old_size=getItemCount();
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView name;
        ImageView img, img_status;
        Button button_del, button_on_off;
        String instrument_id;
        int status, position1;

        public Holder(@NonNull View itemView) {
            super(itemView);
            this.name = itemView.findViewById(R.id.myi_name);
            this.img = itemView.findViewById(R.id.myi_image);
            this.img_status = itemView.findViewById(R.id.yueqizhuangtai);
            this.button_del = itemView.findViewById(R.id.myi_del);
            this.button_on_off = itemView.findViewById(R.id.myi_on_off);
        }

        void bind(int position) {
            position1 = position;
            instrument_id = equipments.get(position).EquipmentNumber;
            status = equipments.get(position).EquipmentStatus;
            name.setText(equipments.get(position).EquipmentName);
            img.setImageURI(Uri.parse(LoadImages.EQUIPMENT_SRC + "/" + equipments.get(position).EquipmentImage));
            int res;
            if (status == 0) {
                button_del.setVisibility(View.INVISIBLE);
                button_on_off.setVisibility(View.INVISIBLE);
                res = R.drawable.red;
            } else if (status == 1) {
                button_on_off.setText(R.string.off);
                res = R.drawable.green;
            } else if (status == 2) {
                button_on_off.setText(R.string.on);
                res = R.drawable.yellow;
            } else {
                button_del.setText(R.string.appeal);
                res = R.drawable.cancel;
            }
            img_status.setImageResource(res);
            button_on_off.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new ChangeThread(instrument_id, status, position1).start();
                }
            });
            button_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new DeleteThread(instrument_id, position1).start();
                }
            });
        }
    }

    public static int f(int type) {
        if (type == 1) return 2;
        else return 1;
    }

    class ChangeThread extends Thread {
        String equipmentNumber;
        int type, position;

        public ChangeThread(String equipmentNumber, int type, int position) {
            this.equipmentNumber = equipmentNumber;
            this.type = f(type);
            this.position = position;
        }

        @Override
        public void run() {
            if (GetResult.ChangeStatusGR(equipmentNumber, type) == 1) {
                equipments.get(position).EquipmentStatus = type;
                changePosition = position;
                Inf.myinstrumentFragment.myHandler.sendEmptyMessage(1);
            }
        }
    }

    class DeleteThread extends Thread {
        String equipmentNumber;
        int position;

        public DeleteThread(String equipmentNumber, int position) {
            this.equipmentNumber = equipmentNumber;
            this.position = position;
            System.out.println(position);
        }

        @Override
        public void run() {
            if (GetResult.DeleteEquipment(this.equipmentNumber) == 1) {
                deletePosition = position;
                equipments.remove(deletePosition);
                Inf.myinstrumentFragment.myHandler.sendEmptyMessage(2);
            }
        }
    }
}

